package com.ruoyi.register.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author 军哥
 * @version 1.0
 * @description: 阿里云OSS管理工具类
 * @date 2022/6/25 11:03
 */

public class AliyunOssUtil {

    private static AliyunOssUtil _instance = null;
    private static OSS _ossClient = null;

    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI4FtqoX9aXnwt5WRPVtJ9";
    private static String accessKeySecret = "BjxfqbWKvpusWM328UGzwYT8PQYlm5";
    private static String bucketName = "shenmazong";
    private static String domain = "https://images.shenmazong.com/";
    private String fileName=null;
    private Long fileSize=0L;
    private String fileUrl=null;

//    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
//    private static String accessKeyId = "LTAI5tRpxdhmiWMLMy7ebTK9";
//    private static String accessKeySecret = "XYhGneMTyvyeHDupk7cYUVjDm8Gqmn";
//    private static String bucketName = "ykl120071";
//    private static String domain = "https://ykl120071.oss-cn-beijing.aliyuncs.com/";

    private AliyunOssUtil() {

    }

    public static AliyunOssUtil OSS() {
        if(_instance == null) {
            _instance = new AliyunOssUtil();
            _ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            return _instance;
        }

        return _instance;
    }

    /**
     * @description 上传本地文件接口
     * @author 军哥
     * @date 2022/6/25 11:35
     * @version 1.0
     */
    public String upload(String fileName) {
        //--1 随机起名
        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String newName = "temp/"+uuid.toString() + suffix;

        String url = null;

        try {
            InputStream inputStream = new FileInputStream(fileName);
            _ossClient.putObject(bucketName, newName, inputStream);

            url = domain + newName;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return url;
    }

    public String upload(MultipartFile file) {
        //--1 判断文件是否有效
        if(file.isEmpty()){
            return null;
        }

        //--2 生成随机文件名
        this.fileName = file.getOriginalFilename();
        //--2 生成随机文件名
        this.fileSize = file.getSize();

        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String newName = "temp/"+uuid.toString() + suffix;

        String url = null;
        try {
            PutObjectResult result = _ossClient.putObject(bucketName, newName, file.getInputStream());
            url = domain + newName;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        this.fileUrl=url;

        return url;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setEndpoint(String endpoint) {
        endpoint = endpoint;
    }

    public void setAccessKeyId(String accessKeyId) {
        accessKeyId = accessKeyId;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        accessKeySecret = accessKeySecret;
    }

    public void setBucketName(String bucketName) {
        bucketName = bucketName;
    }

    public static String getDomain() {
        return domain;
    }

    public static void setDomain(String domain) {
        AliyunOssUtil.domain = domain;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Long getFileSize() {
        return fileSize;
    }

    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }
}